<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="一类" prop="category1">
        <el-select v-model="queryParams.category1" placeholder="请选择一类" clearable>
          <el-option label="出量" value="出量"></el-option>
          <el-option label="入量" value="入量"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二类" prop="category2">
        <el-input
          v-model="queryParams.category2"
          placeholder="请输入二类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三类" prop="category3">
        <el-input
          v-model="queryParams.category3"
          placeholder="请输入三类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否输入基础量" prop="allowBaseInput">
        <el-select v-model="queryParams.allowBaseInput" placeholder="请选择" clearable>
          <el-option label="不可以" :value="0"></el-option>
          <el-option label="可以" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否输入剩余量" prop="allowRemainInput">
        <el-select v-model="queryParams.allowRemainInput" placeholder="请选择" clearable>
          <el-option label="不可以" :value="0"></el-option>
          <el-option label="可以" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="患者录入权限" prop="patientEdit">
        <el-select v-model="queryParams.patientEdit" placeholder="请选择" clearable>
          <el-option label="不可以录入" :value="0"></el-option>
          <el-option label="可以录入" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="医护录入权限" prop="staffEdit">
        <el-select v-model="queryParams.staffEdit" placeholder="请选择" clearable>
          <el-option label="不可以录入" :value="0"></el-option>
          <el-option label="可以录入" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="修改时间" prop="modifiedTime">
        <el-date-picker clearable
          v-model="queryParams.modifiedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:items:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:items:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:items:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:items:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物品编码" align="center" prop="code" />
      <el-table-column label="一类" align="center" prop="category1" />
      <el-table-column label="二类" align="center" prop="category2" />
      <el-table-column label="三类" align="center" prop="category3" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="每单位含水量" align="center" prop="waterContent" />
      <el-table-column label="是否输入基础量" align="center" prop="allowBaseInput">
        <template slot-scope="scope">
          {{ scope.row.allowBaseInput === 1 ? '可以' : '不可以' }}
        </template>
      </el-table-column>
      <el-table-column label="是否输入剩余量" align="center" prop="allowRemainInput">
        <template slot-scope="scope">
          {{ scope.row.allowRemainInput === 1 ? '可以' : '不可以' }}
        </template>
      </el-table-column>
      <el-table-column label="患者录入权限" align="center" prop="patientEdit">
        <template slot-scope="scope">
          {{ scope.row.patientEdit === 1 ? '可以录入' : '不可以录入' }}
        </template>
      </el-table-column>
      <el-table-column label="医护录入权限" align="center" prop="staffEdit">
        <template slot-scope="scope">
          {{ scope.row.staffEdit === 1 ? '可以录入' : '不可以录入' }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="修改时间" align="center" prop="modifiedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:items:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:items:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改管理端可增删改查对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="一类" prop="category1">
          <el-select v-model="form.category1" placeholder="请选择一类">
            <el-option label="出量" value="出量"></el-option>
            <el-option label="入量" value="入量"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二类" prop="category2">
          <el-select v-model="form.category2" placeholder="请选择二类">
            <el-option label="水果" value="水果"></el-option>
            <el-option label="饮品" value="饮品"></el-option>
            <el-option label="餐食" value="餐食"></el-option>
            <el-option label="注射" value="注射"></el-option>
            <el-option label="大便" value="大便"></el-option>
            <el-option label="小便" value="小便"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
          <!-- 当选择"其他"时显示的输入框 -->
          <el-input
            v-if="form.category2 === '其他'"
            v-model="form.category2Custom"
            style="margin-top: 10px"
            placeholder="请输入其他二类"
          />
        </el-form-item>
        <el-form-item label="三类" prop="category3">
          <el-input
            v-model="form.category3"
            placeholder="请输入三类"
            :disabled="!form.category2"
          />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择单位">
            <el-option label="ml" value="ml"></el-option>
            <el-option label="L" value="L"></el-option>
            <el-option label="g" value="g"></el-option>
            <el-option label="kg" value="kg"></el-option>
            <el-option label="个" value="个"></el-option>
            <el-option label="片" value="片"></el-option>
            <el-option label="包" value="包"></el-option>
            <el-option label="袋" value="袋"></el-option>
            <el-option label="瓶" value="瓶"></el-option>
            <el-option label="杯" value="杯"></el-option>
            <el-option label="碗" value="碗"></el-option>
            <el-option label="次" value="次"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
          <!-- 当选择"其他"时显示的输入框 -->
          <el-input
            v-if="form.unit === '其他'"
            v-model="form.unitCustom"
            style="margin-top: 10px"
            placeholder="请输入其他单位"
          />
        </el-form-item>
        <el-form-item label="每单位含水量" prop="waterContent">
          <el-input-number 
            v-model="form.waterContent" 
            :min="0"
            :precision="2"
            :step="0.1"
            placeholder="请输入每单位含水量"
          />
        </el-form-item>
        <el-form-item label="是否输入基础量" prop="allowBaseInput">
          <el-select v-model="form.allowBaseInput" placeholder="请选择">
            <el-option label="不可以" :value="0"></el-option>
            <el-option label="可以" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否输入剩余量" prop="allowRemainInput">
          <el-select v-model="form.allowRemainInput" placeholder="请选择">
            <el-option label="不可以" :value="0"></el-option>
            <el-option label="可以" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="患者录入权限" prop="patientEdit">
          <el-select v-model="form.patientEdit" placeholder="请选择">
            <el-option label="不可以录入" :value="0"></el-option>
            <el-option label="可以录入" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医护录入权限" prop="staffEdit">
          <el-select v-model="form.staffEdit" placeholder="请选择">
            <el-option label="不可以录入" :value="0"></el-option>
            <el-option label="可以录入" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifiedTime">
          <el-date-picker clearable
            v-model="form.modifiedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItems, getItems, delItems, addItems, updateItems } from "@/api/system/items";

export default {
  name: "Items",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管理端可增删改查表格数据
      itemsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        category1: null,
        category2: null,
        category3: null,
        unit: null,
        waterContent: null,
        allowBaseInput: null,
        allowRemainInput: null,
        patientEdit: null,
        staffEdit: null,
        notes: null,
        modifiedTime: null
      },
      // 表单参数
      form: {
        category2Custom: '',
        unitCustom: '',
      },
      // 表单校验
      rules: {
        category1: [
          { required: true, message: "一类不能为空", trigger: "blur" }
        ],
        category2: [
          { required: true, message: "二类不能为空", trigger: "blur" }
        ],
        category3: [
          { required: true, message: "三类不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        waterContent: [
          { required: true, message: "每单位含水量不能为空", trigger: "blur" }
        ],
        allowBaseInput: [
          { required: true, message: "是否输入基础量不能为空", trigger: "blur" }
        ],
        patientEdit: [
          { required: true, message: "患者录入权限不能为空", trigger: "blur" }
        ],
        staffEdit: [
          { required: true, message: "医护录入权限不能为空", trigger: "blur" }
        ],
      },
      // 添加二类和三类的联动数据
      categoryOptions: {
        '饮品': ['水', '茶', '牛奶', '豆浆', '酸奶', '果汁', '饮料', '其他'],
        '餐食': ['肉蔬', '汤类', '米饭', '米粥', '稀饭', '面条', '包子', '馒头', '花卷', '油条', '饺子', '馄饨', '点心', '烙饼', '面包', '蛋糕', '零食', '其他'],
        '水果': ['苹果', '香蕉', '橙子', '梨', '桃', '柚子', '李子', '西瓜', '葡萄', '蓝莓', '草莓', '芒果', '樱桃', '菠萝', '荔枝', '龙眼', '杨梅', '枇杷', '石榴', '山竹', '牛油果', '哈密瓜', '猕猴桃', '火龙果', '车厘子', '其他'],
        '注射': ['输液', '输血', '其他'],
        '大便': ['便秘', '正常便', '糊状便', '水样便', '其他'],
        '小便': ['小便', '其他'],
        '其他': ['呕吐', '咳痰', '咳血', '引流', '高烧', '其他']
      }
    };
  },
  watch: {
    'form.category2'(newVal) {
      this.form.category3 = null;
      if (newVal !== '其他') {
        this.form.category2Custom = '';
      }
    },
    'form.unit'(newVal) {
      if (newVal !== '其他') {
        this.form.unitCustom = '';
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询管理端可增删改查列表 */
    getList() {
      this.loading = true;
      listItems(this.queryParams).then(response => {
        this.itemsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        code: null,
        category1: null,
        category2: null,
        category2Custom: '',
        category3: null,
        unit: null,
        unitCustom: '',
        waterContent: 0,
        allowBaseInput: 1,
        allowRemainInput: 0,
        patientEdit: 1,
        staffEdit: 1,
        notes: null,
        modifiedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.code)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加管理端可增删改查";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const code = row.code || this.ids
      getItems(code).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改管理端可增删改查";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.category2 === '其他' && this.form.category2Custom) {
            this.form.category2 = this.form.category2Custom;
          }
          
          if (this.form.unit === '其他' && this.form.unitCustom) {
            this.form.unit = this.form.unitCustom;
          }
          
          if (this.form.code != null) {
            updateItems(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItems(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const codes = row.code || this.ids;
      this.$modal.confirm('是否确认删除管理端可增删改查编号为"' + codes + '"的数据项？').then(function() {
        return delItems(codes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/items/export', {
        ...this.queryParams
      }, `items_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
/* 可选：为自定义输入框添加样式 */
.el-select {
  width: 100%;
}
</style>
